package controller;

import beans.SocketLog;
import dao.SocketLogDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class SocketLogList extends HttpServlet {
    private static final long serialVersionUID = 5889789111897365340L;
    private SocketLogDao socketLogDao = new SocketLogDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
            Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
            List<Object> results = socketLogDao.logList(onePageCount, currentPage);
            int pageNum = socketLogDao.getLogPageNum(onePageCount);
            JSONObject result = new JSONObject();
            JSONObject content = new JSONObject();
            content.put("logs", JSONArray.fromObject(results));
            content.put("pageNum", pageNum);
            result.put("result", content);
            response.getWriter().write(result.toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            response.getWriter().write("error");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
